﻿/* ******************************************************* */
/* Microsoft CRM 4.0 JavaScript Anpassung                  */
/* NotesCount (v0.5) - CRM-Services 2007                   */
/* netzhaus AG                                             */
/* ******************************************************* */
/* Autor: Thomas Roschinsky (tr@netzhaus.ag)               */
/* Datum: 11.04.2009                                       */
/*                                                         */
/* Funktion:                                               */
/* Beim Laden des jeweiligen Formulars wird mit der ID des */
/* Datensatzes am Webservice nach vorhandenen Notizen ge-  */
/* fragt. Wenn Notizen vorhanden sind, wird der Text auf   */
/* der Lasche eingefärbt und die Anzahl der Notizen ange-  */
/* fügt.                                                   */
/* Zusätzlich kann dabei die Anzahl der Notizen und Datei- */
/* anlagen unterschieden werden (oNotesDetail=true).       */
/* Desweiteren kann beim Überfahren der Lasche mit dem     */
/* Maus-Cursor ein Tooltip eingeblendet werden             */
/* (oNotesOverview=true), der eine Zusammenfassung der     */
/* angehängten Datensätze enthält.                         */
/*                                                         */
/* Konfiguration:                                          */
   var oNotesTab = document.getElementById('tab3Tab');
   var oNotesDetail = true;
   var oNotesDetailColorNote = "#AA4E26";
   var oNotesDetailColorFile = "#3C643A";
   var oNotesOverview = true;
/*                                                         */
/* ******************************************************* */

function displayNotesOverview(e)
{
    try {
        var NotesCountOV = document.getElementById('NotesCountOV');

        var posX = 0;
        var posY = 0;
        if (!e) var e = window.event;
        if (e.pageX || e.pageY)
        {
            posX = e.pageX;
            posY = e.pageY;
        }
        else if (e.clientX || e.clientY)
        {
            posX = e.clientX + document.body.scrollLeft;
            posY = e.clientY + document.body.scrollTop;
        }
        NotesCountOV.firstChild.style.width = NotesCountOV.style.width;
        NotesCountOV.firstChild.style.left = (posX).toString()+"px";
        NotesCountOV.firstChild.style.top = (posY+20).toString()+"px";
        NotesCountOV.style.visibility = (e.type == "mouseleave") ? "hidden" : "visible";
    }
    catch(ex)
    {}    
}

function WSRetrieveMultiple(crmEntity, resultAttribute, queryAttribute, queryValue)
{
    try {
        var oXMLSoapMsg = "<?xml version='1.0' encoding='utf-8'?>"+ 
            "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+
            " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
            " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+ 
            GenerateAuthenticationHeader().toString()+ 
            "<soap:Body>"+ 
            "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ 
            "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'"+
            " xsi:type='q1:QueryExpression'>"+ 
            "<q1:EntityName>"+crmEntity+"</q1:EntityName>"+ 
            "<q1:ColumnSet xsi:type='q1:ColumnSet'>"+ 
            "<q1:Attributes>"+ 
            "<q1:Attribute>"+resultAttribute+"</q1:Attribute>"+ 
            "<q1:Attribute>isdocument</q1:Attribute>"+ 
            "<q1:Attribute>filename</q1:Attribute>"+ 
            "</q1:Attributes>"+ 
            "</q1:ColumnSet>"+ 
            "<q1:Distinct>false</q1:Distinct>"+ 
            "<q1:Criteria>"+ 
            "<q1:FilterOperator>And</q1:FilterOperator>"+ 
            "<q1:Conditions>"+ 
            "<q1:Condition>"+ 
            "<q1:AttributeName>"+queryAttribute+"</q1:AttributeName>"+ 
            "<q1:Operator>Like</q1:Operator>"+ 
            "<q1:Values>"+ 
            "<q1:Value xsi:type='xsd:string'>"+queryValue+"</q1:Value>"+ 
            "</q1:Values>"+ 
            "</q1:Condition>"+ 
            "</q1:Conditions>"+ 
            "</q1:Criteria>"+ 
            "</query>"+ 
            "</RetrieveMultiple>"+ 
            "</soap:Body>"+ 
            "</soap:Envelope>";

        var oXMLHTTPReq = new ActiveXObject("Msxml2.XMLHTTP"); 
        oXMLHTTPReq.onreadystatechange = function(){
            if(oXMLHTTPReq.readyState == 4)
            {
                WSResultHandler(oXMLHTTPReq.responseXML, resultAttribute, queryAttribute);
            }
        };
        oXMLHTTPReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        oXMLHTTPReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
        oXMLHTTPReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        oXMLHTTPReq.setRequestHeader("Content-Length", oXMLSoapMsg.length);
        oXMLHTTPReq.send(oXMLSoapMsg);
    }
    catch(ex)
    {}   
}

function WSResultHandler(resultSetXML, resultAttribute, queryAttribute)
{
    try 
    {
        // Check for errors.
        var errorCount = resultSetXML.selectNodes('//error').length;
        if (errorCount != 0)
        {
            var msg = resultSetXML.selectSingleNode('//description').nodeTypedValue;
            alert(msg);
        }
        else
        {
            var results = resultSetXML.getElementsByTagName('BusinessEntity');
            var resultsOverview = "";
            if (results.length != 0)
            {
                // Split resultset and create div for NotesOverview
                resultsOverview += "<div style=\"z-index:+300; padding:5px; font-size:xx-small; border: solid 1px #C5C5C5; background-color:#FFFFAE; position:absolute; filter:alpha(opacity:90);\">";
                resultsOverview += "<strong>Anlagen und Notizen</strong>";
                resultsOverview += "<table border=\"0\"  style=\"font-size: xx-small\">";
                
                var cntNote = 0;
                var cntFile = 0;
                var lenTitle = 0;
                for (i=0;i < results.length;i++)
                {
                    if(results[i].selectSingleNode('./q1:isdocument').nodeTypedValue == 0)
                    {
                        resultsOverview += "<tr><td><img src=\"/_imgs/ico_16_5_d.gif\" alt=\"Notiz\" /></td><td width=\"100%\" style=\"color:"+oNotesDetailColorNote+";\">"+results[i].selectSingleNode('./q1:'+resultAttribute).nodeTypedValue+"</td></tr>";
                        lenTitle = (lenTitle < results[i].selectSingleNode('./q1:'+resultAttribute).nodeTypedValue.toString().length) ? results[i].selectSingleNode('./q1:'+resultAttribute).nodeTypedValue.toString().length : lenTitle;
                        cntNote++;
                    }
                    else
                    {
                        resultsOverview += "<tr><td><img src=\"/_imgs/ico_16_1001_d.gif\" alt=\"Dateianlage\" /></td><td width=\"100%\" style=\"color:"+oNotesDetailColorFile+";\">"+results[i].selectSingleNode('./q1:filename').nodeTypedValue+"</td></tr>";
                        lenTitle = (lenTitle < results[i].selectSingleNode('./q1:'+resultAttribute).nodeTypedValue.toString().length) ? results[i].selectSingleNode('./q1:filename').nodeTypedValue.toString().length : lenTitle;
                        cntFile++;                        
                    }
                }
                resultsOverview += "</table>";
                resultsOverview += "</div>";
                
                // Build NotesDetail if wanted
                resultsDetails = (oNotesDetail) ? ":&nbsp;<font color=\""+oNotesDetailColorNote+"\">"+cntNote.toString()+"</font>/<font color=\""+oNotesDetailColorFile+"\">"+cntFile.toString()+"</font>" : "";
                
                // Add color an counters to tab oNotesTab
                oNotesTab.firstChild.style.color = '#EE6000';
                oNotesTab.firstChild.innerHTML += ' (<strong>' + results.length.toString() + '</strong>'+resultsDetails+')';
                oNotesTab.parentNode.style.width = (parseInt(oNotesTab.parentNode.style.width.substring(0, oNotesTab.parentNode.style.width.indexOf("px")))+65).toString() + 'px';

                if(oNotesOverview)
                {
                    // Add new div containing the NotesOverview
                    var divNotesCountOV = document.createElement("div");
                    divNotesCountOV.setAttribute('id', 'NotesCountOV');
                    divNotesCountOV.style.visibility = "hidden";
                    divNotesCountOV.style.width = (lenTitle <= 70) ? parseInt((lenTitle * 6)).toString() + "px" : "420px";
                    document.body.appendChild(divNotesCountOV);
                    divNotesCountOV.innerHTML = divNotesCountOV.innerHTML+resultsOverview;
                    // Add mouse events to notes tab link to show and hide the NotesOverview
                    oNotesTab.firstChild.attachEvent('onmouseenter', displayNotesOverview);
                    oNotesTab.firstChild.attachEvent('onmouseleave', displayNotesOverview);
                }
            }
        }
    }
    catch(e)
    {}
}

if(crmForm.ObjectId!=null)
{
    WSRetrieveMultiple('annotation', 'subject', 'objectid', crmForm.ObjectId.toString());
}

/* END *************************************************** */
/* NotesCount                                              */
/* ******************************************************* */